<?php
$platformNumber = '10442';

$_GET['pwdcode'] = md5(date("YmdHi").$platformNumber);

function validateCode($platformNumber) {
	$result = false;
	if(isset($_GET['pwdcode']) && !empty($_GET['pwdcode'])) {
		$code = $_GET['pwdcode'];
		$validate = array(
			md5(date("YmdHi",time()+120).$platformNumber),
			md5(date("YmdHi",time()+60).$platformNumber),
			md5(date("YmdHi").$platformNumber),
			md5(date("YmdHi",time()-60).$platformNumber),
			md5(date("YmdHi",time()-120).$platformNumber),
			md5(date("YmdHi",time()-180).$platformNumber),
		);
		if(in_array($code,$validate)) {
			$result = true;
		}
	}
	return $result;
}

if(!validateCode($platformNumber)) {
	echo "error";exit;
}

	/**
	 * 数据库连接
	 */
		include 'db.php';
		
	/**
	 * 数据缓存
	 */
	include 'mymc.php';
	$cache = new Mymc();
	$key = 'api_borrow_now_all';
	$list = $cache->get_array($key);
	
	/**
	 * 获取参数数据 GET方式
	 * @var status :  ALL 所有 ; ONGOING  正在进行中 
	 * @var ids :  标的唯一标识ID, 查询多个时使用的是逗号, 如：ids=1,2,3 
	 * @var startTimeA : 投标开始时间 大于等于 startTimeA , 日期格式yyyyMMddHHmmss,如：startTimeA = 20120910232851
	 * @var startTimeB : 投标开始时间 小于等于 startTimeB,  日期格式yyyyMMddHHmmss,如：startTimeB = 20120911232851
	 * @var endTimeA : 投标结束时间 大于等于 endTimeA , 日期格式yyyyMMddHHmmss,如：endTimeA = 20120910232851
	 * @var endTimeB : 投标结束时间 小于等于 endTimeB,  日期格式yyyyMMddHHmmss,如：endTimeB = 20120911232851
	 */
	
	$querySQL = "";
	if(isset($_GET['status']) && $_GET['status']=='ALL'){
		$where = array();
		if(isset($_GET['ids']) && !empty($_GET['ids'])) {
			$where[] = "`id` in (".$_GET['ids'].")"; 
		}
		
		if(isset($_GET['startTimeA']) && !empty($_GET['startTimeA'])) {
			$where[] = "`verify_time` >= ".strtotime($_GET['startTimeA']); 
		}
		
		if(isset($_GET['startTimeB']) && !empty($_GET['startTimeB'])) {
			$where[] = "`verify_time` <= ".strtotime($_GET['startTimeB']); 
		}
		
		if(isset($_GET['endTimeA']) && !empty($_GET['endTimeA'])) {
			$where[] = "(ftype!=4 && repayment_time>=strtotime("+$_GET['endTimeA']+")) or (ftype=4 && lz_endtime>=strtotime("+$_GET['endTimeA']+"))"; 
		}	
		if(isset($_GET['endTimeB']) && !empty($_GET['endTimeB'])) {
			$where[] = "(ftype!=4 && repayment_time<=strtotime("+$_GET['endTimeA']+")) or (ftype=4 && lz_endtime<=strtotime("+$_GET['endTimeA']+"))";
		}
		
		if(!empty($where)) {
			$querySQL .= " ".implode(" and ",$where);//按请求的查询条件查询
		} else {
			$querySQL .= " `fstatus`=1";//默认查询:状态为 正在进行中的标
		}
		
	}else{
		$querySQL .= " `fstatus`=1";//默认查询:状态为 正在进行中的标
	}	

	include 'functions.php';
	
	if ($list==false)
	{
		$sql = "select * from ych_borrow where ".$querySQL;
		$list = db_fetch_arrays($sql,$conn);
		$cache->set_array($key, $list, 600);
	}
	
	/**
	 * 生成xml格式
	 */
	header('Content-Type: text/xml;');
	$dom = new DOMDocument('1.0', 'gbk');
	$borrowInfos = $dom->createElement('bidInfos');
	$dom->appendChild($borrowInfos);
	
	if($list)
	{
		foreach ($list as $val)
		{
			$borrowInfo = $dom->createElement('bidInfo');
			$borrowInfos->appendChild($borrowInfo);
			
			//网贷平台编号,由网贷中心给定 暂时生成5位随机数
			//$number = rand(10000,20000);
			$platformNumberbid = $dom->createElement('platformNumber');
			$platformNumberText = $dom->createTextNode($platformNumber);
			$platformNumberbid->appendChild($platformNumberText);
			$borrowInfo->appendChild($platformNumberbid);
			
			//此标的ID，必填且唯一，用于标识这个标
			$id = $dom->createElement('id');
			$idText = $dom->createTextNode($val["id"]);
			$id->appendChild($idText);
			$borrowInfo->appendChild($id);
			
			//标题
			$title = $dom->createElement('title');
			$titleText = $dom->createTextNode($val["title"]);
			$title->appendChild($titleText);
			$borrowInfo->appendChild($title);
			
			//借款人
			$borrower = $dom->createElement('borrower');
			$sql = "select username from ych_user where user_id=".$val["user_id"];
			$row = db_fetch_array($sql,$conn);
			$username = isset($row["username"]) ? $row["username"] : "***";
			$borrowerText = $dom->createTextNode($username);
			$borrower->appendChild($borrowerText);
			$borrowInfo->appendChild($borrower);
			
			//查看此标的url地址
			$url = $dom->createElement('url');
			$tmpstr = "http://www.gsdai.com/borrow/obj/".$val["id"];
			$urlText = $dom->createTextNode($tmpstr);
			$url->appendChild($urlText);
			$borrowInfo->appendChild($url);
			
			//借款类型，只能是秒、押、荐、净、流、担、信中的一种
			$txttype='';
			if($val["ftype"]==2){
				$txttype = "押";
			}elseif($val["ftype"]==5){
				$txttype = "秒";
			}elseif($val["ftype"]==3){
				$txttype = "净";
			}elseif($val["ftype"]==6){
				$txttype = "担";
			}elseif($val["ftype"]==4){
				$txttype = "流";
			}elseif($val["ftype"]==1){
				$txttype = "信";
			}else{
				$txttype = "荐";
			}
			
			$type = $dom->createElement('type');
			$typeText = $dom->createTextNode($txttype);
			$type->appendChild($typeText);
			$borrowInfo->appendChild($type);
		
			//借款金额，单位元
			$amount = $dom->createElement('amount');
			$amountText = $dom->createTextNode(intval($val["account"]));
			$amount->appendChild($amountText);
			$borrowInfo->appendChild($amount);
			
			//年利率,保留两位小数，例如：22.26
			$annualInterestRate = $dom->createElement('annualInterestRate');
			$annualInterestRateText = $dom->createTextNode($val["apr"]);
			$annualInterestRate->appendChild($annualInterestRateText);
			$borrowInfo->appendChild($annualInterestRate);
			
			//投标奖励百分比(额外收益)，保留两位小数，例如：9.65
			$prize = $dom->createElement('prize');
			$prizeText = $dom->createTextNode($val["funds"]);
			$prize->appendChild($prizeText);
			$borrowInfo->appendChild($prize);
			
			//此标管理费百分比，例如10%的管理费，此处填10
			$managementFee = $dom->createElement('managementFee');
			$managementFeeText = $dom->createTextNode(10);
			$managementFee->appendChild($managementFeeText);
			$borrowInfo->appendChild($managementFee);
			
			//借款期限单位，月、天; 填写 月 或者 天
			$timeUnit = $dom->createElement('timeUnit');
			$timetext = $val["is_day"] == 1 ? '天':'月';
			$timeUnitText = $dom->createTextNode($timetext);
			$timeUnit->appendChild($timeUnitText);
			$borrowInfo->appendChild($timeUnit);
			
			//借款期限数，如：三个月，此处就是3；如果是18天，就是写18
			$limitTime = $dom->createElement('limitTime');
			$limittext = $val["is_day"] == 1 ?$val["time_limit_day"]:$val["time_limit"];
			$limitTimeText = $dom->createTextNode($limittext);
			$limitTime->appendChild($limitTimeText);
			$borrowInfo->appendChild($limitTime);
			
			//还款方式，如果是按月等额本息，此处填1；如果是按季还款,此处填2；按月付息-到期还本，填3；到期还本息，填4
			$repaymentType = $dom->createElement('repaymentType');
			$repayment='';
			if($val["style"]==3){
				$repayment = "3";
			}else{
				$repayment = "1";
			}
			$repaymentTypeText = $dom->createTextNode($repayment);
			$repaymentType->appendChild($repaymentTypeText);
			$borrowInfo->appendChild($repaymentType);
			
			//此标当前完成进度百分比，保留两位小数，例如66.16
			$process = $dom->createElement('process');
			$txtprocess  = sprintf("%.2f", intval($val["account_yes"]/$val["account"]*10000)/100);
			$processText = $dom->createTextNode($txtprocess);
			$process->appendChild($processText);
			$borrowInfo->appendChild($process);
			
			//最小投资金额（单位元）
			$minBidAmount = $dom->createElement('minBidAmount');
			$minBidAmountText = $dom->createTextNode(intval($val["lowest_account"]));
			$minBidAmount->appendChild($minBidAmountText);
			$borrowInfo->appendChild($minBidAmount);
			
			//最大投资金额（单位元）(没有限制则填写：不限)
			$maxBidAmount = $dom->createElement('maxBidAmount');
			$mosttext = intval($val["most_account"])>0 ?intval($val["most_account"]):'不限';
			$maxBidAmountText = $dom->createTextNode($mosttext);
			$maxBidAmount->appendChild($maxBidAmountText);
			$borrowInfo->appendChild($maxBidAmount);
			
			//剩余可投金额(单位元)(已结束则填写：0)
			$surplusBidAmount = $dom->createElement('surplusBidAmount');
			$surplusamount = intval($val["account"]) - intval($val["account_yes"]);
			$surplusBidAmountText = $dom->createTextNode($surplusamount);
			$surplusBidAmount->appendChild($surplusBidAmountText);
			$borrowInfo->appendChild($surplusBidAmount);
			
			//已投标笔数,(加入此标多少人次)
			$bidsCount = $dom->createElement('bidsCount');
			$bidsCountText = $dom->createTextNode($val['tender_times']);
			$bidsCount->appendChild($bidsCountText);
			$borrowInfo->appendChild($bidsCount);
			
			//是否是定向标或密码标，是则填1，否则填0
			$isDirectional = $dom->createElement('isDirectional');
			$isDirectionalText = $dom->createTextNode(intval($val['is_dxb']));
			$isDirectional->appendChild($isDirectionalText);
			$borrowInfo->appendChild($isDirectional);
			
			//此标开始时间，格式是yyyyMMddHHmmss
			$startTime = $dom->createElement('startTime');
			$verify_time = date('YmdHis', $val["verify_time"]);
			$startTimeText = $dom->createTextNode($verify_time);
			$startTime->appendChild($startTimeText);
			$borrowInfo->appendChild($startTime);
			
			//此标结束时间，格式是yyyyMMddHHmmss。正在投的标，此处不用填
			$endTime = $dom->createElement('endTime');
			$endTimeText = $dom->createTextNode("");
			$endTime->appendChild($endTimeText);
			$borrowInfo->appendChild($endTime);
		
		}
	}	
	$dom->formatOutput = true;
	$xmlString = $dom->saveXML();
	echo $xmlString;
	
?>